package com.tchintech.pay.view.activity;

import android.os.Bundle;
import android.test.ApplicationTestCase;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.tchintech.pay.R;
import com.tchintech.pay.delegate.QuickOrderActivityDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.utils.AppConst;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/6 0006.
 */

public class QuickOrderActivity extends ActivityPresenter<QuickOrderActivityDelegate> implements View.OnClickListener {

    private WebView webView;
    private Map<String, String> map = new HashMap<String, String>();

    @Override
    public void onClick(View v) {

    }

    @Override
    protected Class<QuickOrderActivityDelegate> getDelegateClass() {
        return QuickOrderActivityDelegate.class;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        webView = viewDelegate.getid(R.id.quickOrder_wv);
        initWebview(webView);

    }


    private void initWebview(WebView webView) {
        WebSettings webSettings = webView.getSettings();
        webSettings.setSaveFormData(false);
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setDefaultTextEncodingName("UTF-8");//设置默认为utf-8
        webView.setHorizontalScrollBarEnabled(false);
        webView.setVerticalScrollBarEnabled(false);
        webView.addJavascriptInterface(new JavaScriptInterface(), "js");
        webView.setWebViewClient(new WebViewClient() {
            // 重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            // 重写此方法可以让webview处理https请求
            @Override
            public void onReceivedSslError(WebView view,
                                           SslErrorHandler handler, android.net.http.SslError error) {
                handler.proceed();
            }
        });

        webView.loadUrl("file:///android_asset/html/quickOrderCreate.html");

    }

    public class JavaScriptInterface {
        @JavascriptInterface
        public void getMessage(String amount, String payerAcc, String productName, String productDesc) {
            map = new HashMap<>();
            map.put("amount", amount);
            map.put("payerAcc", payerAcc);
            map.put("productName", productName);
            map.put("productDesc", productDesc);

            try {
                //HttpUtils.doPost(getHoldingActivity(), url, map, QuickOrderCreateFragment.this, 123, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @JavascriptInterface
        public void toast(String str) {
            showMsg(str);
        }
    }
}
